import  requests
from multiprocessing.dummy import Pool
import os
import re
from lxml import etree

os.mkdir('./视频')
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
begin_url = 'https://www.pearvideo.com/category_5'
page_text = requests.get(url=begin_url,headers=headers).text
# print(page_text)
detail_url_list = re.findall('<a href="(video_\d+?)"',page_text)
# print(detail_url_list)
def send_detail_url(url):
    detail_url = 'https://www.pearvideo.com/'+ url
    detail_text = requests.get(url=detail_url,headers=headers).text
    # print(detail_text)
    tree = etree.HTML(detail_text)
    video_url = re.findall('srcUrl="(.+?\.mp4)"',detail_text)[0]
    video_name = tree.xpath('//*[@id="detailsbd"]/div[1]/div[2]/div/div[1]/h1/text()')[0]
    video_page = requests.get(url=video_url,headers=headers).content
    video_path = './视频/' + video_name + '.mp4'
    with open(video_path,'wb') as f:
         f.write(video_page)
         print(video_name,"保存成功")
for url in detail_url_list:
    send_detail_url(url)
pool = Pool(4)
pool.map(send_detail_url,detail_url_list)

pool.close()
pool.join()
